"""获取散装视频的接口信息"""
import re

from lxml import etree
from bilibili_classify import browser_set
import requests
import json
import redis

with open('../json/bilibili_bulk.json', 'r') as f:
    all_data = json.loads(f.read())  # 读取所有散装视频分类信息

redis_conn = redis.StrictRedis()  # 创建redis数据库连接


def get_bluk_info():
    """获取接口信息"""
    header = {
    }  # 请求头配置
    # 访问请求
    data_lsit = []  # 用于存储信息的空列表
    for result in all_data:
        title, classify, url = result['title'], result['classify'], result['url']
        print(title, classify, url)
        try:
            if title == "公开课":
                response = browser_set(url)  # 这部分开科技！可能会很慢！
            else:
                response = requests.get(url, headers=header).content.decode('utf-8')
        except Exception as e:
            print(e)
        root = etree.HTML(response)  # 解析网址
        result = "".join(root.xpath(''))  # 获取结果
        if result:
            re_data = re.findall(r'', result, re.S)
            if re_data:
                cate_id = int(re_data[0])  # 获取接口id
                data = {
                    "title": title,
                    "classify": classify,
                    "cate_id": cate_id,
                }
                data_lsit.append(data)
                redis_conn.rpush('', json.dumps(data, ensure_ascii=False, indent=4))
    with open('../json/bilibili_bulk_API.json', 'a') as f:
        f.write(json.dumps(data_lsit, ensure_ascii=False, indent=4))

# if __name__ == '__main__':
#     get_bluk_info()
